home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / StringWriter.java < prev    next >
Text File  |  1998-09-22  |  3KB  |  113 lines

  1. /*
  2.  * @(#)StringWriter.java    1.6 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * A character stream that collects its output in a string buffer, which can
  20.  * then be used to construct a string.
  21.  *
  22.  * @version     1.6, 98/07/01
  23.  * @author    Mark Reinhold
  24.  * @since    JDK1.1
  25.  */
  26.  
  27. public class StringWriter extends Writer {
  28.  
  29.     private StringBuffer buf;
  30.  
  31.     /**
  32.      * Create a new string writer, using the default initial string-buffer
  33.      * size.
  34.      */
  35.     public StringWriter() {
  36.     buf = new StringBuffer();
  37.     lock = buf;
  38.     }
  39.  
  40.     /**
  41.      * Create a new string writer, using the specified initial string-buffer
  42.      * size.
  43.      */
  44.     protected StringWriter(int initialSize) {
  45.     buf = new StringBuffer(initialSize);
  46.     lock = buf;
  47.     }
  48.  
  49.     /**
  50.      * Write a single character.
  51.      */
  52.     public void write(int c) {
  53.     buf.append((char) c);
  54.     }
  55.  
  56.     /**
  57.      * Write a portion of an array of characters.
  58.      *
  59.      * @param  cbuf  Array of characters
  60.      * @param  off   Offset from which to start writing characters
  61.      * @param  len   Number of characters to write
  62.      */
  63.     public void write(char cbuf[], int off, int len) {
  64.     buf.append(cbuf, off, len);
  65.     }
  66.  
  67.     /**
  68.      * Write a string.
  69.      */
  70.     public void write(String str) {
  71.     buf.append(str);
  72.     }
  73.  
  74.     /**
  75.      * Write a portion of a string.
  76.      *
  77.      * @param  str  String to be written
  78.      * @param  off  Offset from which to start writing characters
  79.      * @param  len  Number of characters to write
  80.      */
  81.     public void write(String str, int off, int len)  {
  82.     char cbuf[] = new char[len];
  83.     str.getChars(off, len, cbuf, 0);
  84.     buf.append(cbuf);
  85.     }
  86.  
  87.     /**
  88.      * Return the buffer's current value as a string.
  89.      */
  90.     public String toString() {
  91.     return buf.toString();
  92.     }
  93.  
  94.     /**
  95.      * Return the string buffer itself.
  96.      */
  97.     public StringBuffer getBuffer() {
  98.     return buf;
  99.     }
  100.  
  101.     /**
  102.      * Flush the stream.
  103.      */
  104.     public void flush() { }
  105.  
  106.     /**
  107.      * Close the stream.  This method does not release the buffer, since its
  108.      * contents might still be required.
  109.      */
  110.     public void close() { }
  111.  
  112. }
  113.